package cn.larry.common.utils;

/**
 * llin 2020年1月8日上午9:38:02
 */

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @author llin 汉语拼音工具
 */
public class HanyuPinyinUtil {

	/*public static void main(String[] args) throws Exception {
		String pinyin = "";

		String name = "中文s222拼ss音";
		pinyin = pinyinInitial(name, null);

		System.out.println(pinyin);
		System.out.println(pinyinAllInitial(name, UPPERCASE));
	}*/


    /**
     * The option indicates that hanyu pinyin is outputted as uppercase letters
     */
    public static final HanyuPinyinCaseType UPPERCASE = HanyuPinyinCaseType.UPPERCASE;

    /**
     * The option indicates that hanyu pinyin is outputted as lowercase letters
     */
    public static final HanyuPinyinCaseType LOWERCASE = HanyuPinyinCaseType.LOWERCASE;


    /**
     * 获取汉字拼音的首字母
     *
     * @param name
     *            中文汉字（非汉字不转换）
     * @param caseType
     *            设置大小写格式(default：大写)
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    public static String pinyinInitial(String name, HanyuPinyinCaseType caseType)
        throws BadHanyuPinyinOutputFormatCombination {

        if (caseType == null)
            caseType = UPPERCASE;

        char[] charArray = name.toCharArray();
        StringBuilder pinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 设置大小写格式
        defaultFormat.setCaseType(caseType);
        // 设置声调格式：
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < charArray.length; i++) {
            // 匹配中文,非中文转换会转换成null
            if (Character.toString(charArray[i]).matches("[\\u4E00-\\u9FA5]+")) {
                String[] hanyuPinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(charArray[i], defaultFormat);
                if (hanyuPinyinStringArray != null) {
                    pinyin.append(hanyuPinyinStringArray[0].charAt(0));
                }
            }
        }
        return pinyin.toString();
    }

    /**
     * 获取汉字拼音
     *
     * @param name
     *            中文汉字（非汉字不转换）空格间隔
     * @param caseType
     *            设置大小写格式(default：大写)
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    public static String pinyin(String name, HanyuPinyinCaseType caseType)
        throws BadHanyuPinyinOutputFormatCombination {

        if (caseType == null)
            caseType = UPPERCASE;

        char[] charArray = name.toCharArray();
        StringBuilder pinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 设置大小写格式
        defaultFormat.setCaseType(caseType);
        // 设置声调格式：
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < charArray.length; i++) {
            // 匹配中文,非中文转换会转换成null
            if (Character.toString(charArray[i]).matches("[\\u4E00-\\u9FA5]+")) {
                String[] hanyuPinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(charArray[i], defaultFormat);
                String string = hanyuPinyinStringArray[0];
                pinyin.append(string + " ");
            } else {
                pinyin.append(charArray[i]);
            }
        }
        return pinyin.toString().trim();
    }

    /**
     * 获取汉字拼音的首字母
     *
     * @param name
     *            中文汉字（非汉字取原数据）
     * @param caseType
     *            设置大小写格式(default：大写)
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    public static String pinyinAllInitial(String name, HanyuPinyinCaseType caseType)
        throws BadHanyuPinyinOutputFormatCombination {

        char[] charArray = name.toCharArray();
        StringBuilder pinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 设置大小写格式
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        // 设置声调格式：
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < charArray.length; i++) {
            // 匹配中文,非中文转换会取原数据
            if (Character.toString(charArray[i]).matches("[\\u4E00-\\u9FA5]+")) {
                String[] hanyuPinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(charArray[i], defaultFormat);
                if (hanyuPinyinStringArray != null) {
                    pinyin.append(hanyuPinyinStringArray[0].charAt(0));
                }
            } else {
                pinyin.append(charArray[i]);
            }
        }
        String p = pinyin.toString();
        if (caseType == LOWERCASE)
            p = p.toLowerCase();
        else
            p = p.toUpperCase();

        return p;
    }

}
